package cn.com.twoke.easyproject.aspect.request.lock.hook.after;

import cn.com.twoke.easyproject.aspect.request.lock.RequestLock;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;
import org.aspectj.lang.ProceedingJoinPoint;

import java.util.function.Function;

/**
 * 请求锁key的压缩处理器
 */
@RequiredArgsConstructor
@Data
@Accessors(chain = true)
public class GenerateKeyAfterCompressor implements GenerateKeyHook {

    private long max = 255;

    private final Function<String, String> compressor;

    /**
     * 当请求锁生成的key长度大于{@code max}值时进行压缩
     * @param key
     * @param joinPoint
     * @param requestLock
     * @return
     */
    @Override
    public String doAfter(String key, ProceedingJoinPoint joinPoint, RequestLock requestLock) {
        // 如果超过最大限制值进行压缩
        if (key.length() > max) {
            return compressor.apply(key);
        }
        return key;
    }


}
